/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.lookup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.Dept;
import model.inventory.Item;
import model.inventory.ItemSupplier;
import model.inventory.Packaging;
import model.inventory.Principal;
import model.inventory.Unit;
import model.publics.Supplier;
import utility.Constants;

/**
 *
 * @author jay
 */
public class LookupSimpleTM extends AbstractTableModel {
    
    private int lookupId;
    private List<Object> data;

    public LookupSimpleTM(List<Object> data, int id) {
        this.data = data;
        this.lookupId = id;
    }

    public LookupSimpleTM(int id) {
        this(new ArrayList<>(0), id);
    }

    public Object get(int index) {
        return data.get(index);
    }

    public void add(Object product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, Object product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<Object> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: 
            switch (lookupId) {
                case Constants.LOOKUP_DEPT: return ((Dept) data.get(rowIndex)).getKode();
                case Constants.LOOKUP_UNIT: return ((Unit) data.get(rowIndex)).getKode();
                case Constants.LOOKUP_PACKAGE: return ((Packaging) data.get(rowIndex)).getKode();
                case Constants.LOOKUP_PRINCIPAL: return ((Principal) data.get(rowIndex)).getKode();
                case Constants.LOOKUP_SUPPLIER: return ((Supplier) data.get(rowIndex)).getKode();
                case Constants.LOOKUP_ITEM: return ((Item) data.get(rowIndex)).getPlu();
                case Constants.LOOKUP_MAIN_SUPPLIER: return ((ItemSupplier) data.get(rowIndex)).getSupplier().getKode();
                default: return null;    
            }
                
            case 1: 
            switch (lookupId) {
                case Constants.LOOKUP_DEPT: return ((Dept) data.get(rowIndex)).getNama();
                case Constants.LOOKUP_UNIT: return ((Unit) data.get(rowIndex)).getNama();
                case Constants.LOOKUP_PACKAGE: return ((Packaging) data.get(rowIndex)).getNama();
                case Constants.LOOKUP_PRINCIPAL: return ((Principal) data.get(rowIndex)).getNama();
                case Constants.LOOKUP_ITEM: return ((Item) data.get(rowIndex)).getNama();
                case Constants.LOOKUP_SUPPLIER: return ((Supplier) data.get(rowIndex)).getNama();
                case Constants.LOOKUP_MAIN_SUPPLIER: return ((ItemSupplier) data.get(rowIndex)).getSupplier().getNama();
                default: return null;    
            }
            default: return null;
        }
    }
    
    @Override
    public String getColumnName(int column) {
        switch (column) {
            //case 0: return "ID";
            case 0: return "KODE";
            case 1: return "NAMA";
            default: return null;
        }
    }
    
}
